"""
 -*- coding: utf-8-*-
 @File: page_base.py
 @Author: chen
 @time: 2023/10/04/ 18:05
"""
import os.path

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from hogwartsHomework.homework_app_auto_L3.utils.mgt_exception import record_exception
from hogwartsHomework.homework_app_auto_L3.utils.mgt_log import logger
from hogwartsHomework.homework_app_auto_L3.utils.mgt_yaml import MgtYaml

cur_path = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(cur_path, "../config/app_config.yaml")


class BasePage:
    __BASE_URL = "http://127.0.0.1:4723/wd/hub"
    __BASE_CAP = MgtYaml(config_file).get_cap("wework")
    __BLACK_LIST = []

    def __init__(self, driver=None):
        if driver:
            self.driver = driver
        else:
            self.driver = webdriver.Remote(self.__BASE_URL, self.__BASE_CAP)

        self.driver.implicitly_wait(10)

    @record_exception
    def do_find(self, locator: tuple):
        """获取单个元素"""
        element = self.driver.find_element(*locator)
        logger.info(f"查找元素：{locator}--->成功")
        return element

    @record_exception
    def do_finds(self, locator: tuple):
        elements = self.driver.find_elements(*locator)
        if elements:
            # logger.info(f"查找元素：{locator}--->成功")
            return elements
        else:
            logger.error(f"查找元素：{locator}--->失败")
            return None

    @record_exception
    def do_click(self, locator: tuple, i=0):
        """点击元素"""
        self.wait_until_clickable(locator)
        self.do_finds(locator)[i].click()
        logger.info(f"点击元素：{locator}--->成功")

    @record_exception
    def do_input(self, locator: tuple, content: str, i=0):
        """输入内容"""
        self.wait_until_visibility(locator)
        self.do_finds(locator)[i].clear().send_keys(content)
        logger.info(f"输入内容：{content}--->成功")

    @record_exception
    def do_quit(self):
        """退出浏览器进程"""
        self.driver.quit()
        logger.info("退出浏览器")

    @record_exception
    def do_tap(self, amount: list[tuple[int, int]]):
        self.driver.tap(amount)
        logger.info("模拟点击成功")

    # @record_exception
    def wait_until_visibility(self, locator: tuple):
        """显示等待元素可以被定位"""
        WebDriverWait(self.driver, 20).until(
            expected_conditions.visibility_of_element_located(locator)
        )
        logger.info(f"等待元素：{locator}可定位--->成功")

    def wait_until(self, locator):
        WebDriverWait(self.driver, 20).until(
            expected_conditions.visibility_of_any_elements_located(locator)
        )
        logger.info(f"等待元素：{locator}可定位--->成功")

    # @record_exception
    def wait_until_clickable(self, locator: tuple):
        """显示等待元素可以被点击"""
        e = self.do_find(locator)
        WebDriverWait(self.driver, 20).until(
            expected_conditions.element_to_be_clickable(e)
        )
        logger.info(f"等待元素：{locator}可点击--->成功")

    def do_move(self, y1, y2):
        """上下滑动"""
        # 获取屏幕大小
        width = self.driver.get_window_size()['width']
        height = self.driver.get_window_size()['height']
        # 屏幕从下向上滑动(方向：滑到底部）
        # 宽度x:居中不变，均为width*0.5
        # 高度y:从最高位置height*0.9到最低位置height*0.1
        # duration:1000  滑动保证稳定性
        self.driver.swipe(width * 0.5, height * y1, width * 0.5, height * y2, 1000)
        logger.info("滑动界面--->成功")
